// https://www.luogu.com.cn/problem/B3642

#include <iostream>
#include <vector>
using namespace std;

int n;
vector<int> leftChildren, rightChildren;

void preorder(const int i) {
    if (i != 0) {
        printf("%d ", i);
        preorder(leftChildren[i]);
        preorder(rightChildren[i]);
    }
}

void inorder(const int i) {
    if (i != 0) {
        inorder(leftChildren[i]);
        printf("%d ", i);
        inorder(rightChildren[i]);
    }
}

void postorder(const int i) {
    if (i != 0) {
        postorder(leftChildren[i]);
        postorder(rightChildren[i]);
        printf("%d ", i);
    }
}

int main() {
    int left, right;
    scanf("%d", &n);
    leftChildren = vector<int>(n+1), rightChildren = vector<int>(n+1);
    for (int i=1; i<=n; i++) {
        scanf("%d%d", &left, &right);
        leftChildren[i] = left;
        rightChildren[i] = right;
    }

    preorder(1);
    printf("\n");
    inorder(1);
    printf("\n");
    postorder(1);
    return 0;
}